﻿using System;
using System.IO;
using System.Reflection;
using System.Text;

namespace YAMB.Common
{
    public class PathingLogger
    {
        private readonly string path;

        public PathingLogger(string name)
        {
            Name = name;

            path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Logs";

            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }


            path += "\\" + DateTime.Today.ToShortDateString() + ".txt";
        }

        public string Name { get; set; }

        public void Log(string message)
        {
            var sb = new StringBuilder();

            sb.AppendFormat("[{0}] {1}: {2}{3}", DateTime.Now.ToShortTimeString(), Name, message, Environment.NewLine);
            File.AppendAllText(path, sb.ToString());

            Console.Write(sb.ToString());
        }

        public void Log(string message, params object[] args)
        {
            Log(String.Format(message, args));
        }

        public void Log(Exception e)
        {
            Log(String.Format("{0}{1}", e, Environment.NewLine));
        }

        public void Log(Exception e, string message)
        {
            Log(String.Format("{0}{1}{2}{1}", message, Environment.NewLine, e));
        }

        public void Log(Exception e, string format, params object[] args)
        {
            Log(e, String.Format(format, args));
        }
    }
}